import config from "@/request/config.js"
let baseUrl = config.baseUrl

export default {
	// 全局配置
	common: {
		// baseUrl: "http://47.93.222.14:20044",
		// baseUrl: "http://192.168.0.22:20023",

		// 'Content-Type':'application/x-www-form-urlencoded',
		header: {
			'Content-Type': 'application/json',
			AUTHORIZATION: uni.getStorageSync('token')
		},
		data: {},
		method: 'GET',
		dataType: 'json'
	},

	// 请求 返回promise
	request(options = {}) {
		// 组织参数
		options.url = baseUrl.baseUrl + options.url
		options.header = options.header || this.common.header
		options.data = options.data || this.common.data
		options.header.AUTHORIZATION = uni.getStorageSync('token') || options.data.AUTHORIZATION
		// options.header["Content-Type"] = !options.data.czType ? 'application/json' : 'application/x-www-form-urlencoded'
		// options.data.token = uni.getStorageSync('token')||options.data.token;
		options.method = options.method || this.common.method
		options.dataType = options.dataType || this.common.dataType
		return new Promise((res, rej) => {
			// 请求之前... todo
			// 请求中...
			uni.request({
				...options,
				success: (result) => {
					// if(result.data.code==404){
					// 	uni.showToast({
					// 		duration:2000,
					// 		title:result.data.msg,
					// 		icon:'none'
					// 	})
					// 	setTimeout(function() {
					// 		uni.reLaunch({
					// 			url:'/pages/login/index'
					// 		})
					// 	}, 2000);
					// }
					if (result.data.code != 200) {
						if (result.data.code == 401) {
							uni.clearStorage()
							uni.reLaunch({
								url: '/pages/login/index'
							})
						} else {
							uni.showToast({
								duration: 2000,
								title: result.data.msg,
								icon: 'none'
							})
						}
					}
					if (result.data.code == 200) {
						return res(result.data);
					}
				},
				fail: (error) => {
					// 取消加载
					uni.hideLoading();
					// 提示错误
					uni.showToast({
						title: '网络连接失败',
						icon: 'none'
					});
					return rej()
				}
			});
		}).catch((e) => {});
	},
	// get请求2
	getTwo(url, data = {}, options = {}) {
		options.url = url + '/' + data
		options.method = 'get'
		return this.request(options)
	},
	// get请求
	get(url, data = {}, options = {}) {
		options.url = url
		options.data = data
		options.method = 'GET'
		return this.request(options)
	},
	// post请求
	post(url, data = {}, options = {}) {
		options.url = url
		options.data = data
		options.method = 'POST'
		return this.request(options)
	},
	// delete请求
	del(url, data = {}, options = {}) {
		options.url = url + '/' + data
		options.method = 'DELETE'
		return this.request(options)
	},
	// options请求
	options(url, data = {}, options = {}) {
		options.url = url
		options.data = data
		options.method = 'options'
		return this.request(options)
	},
	// put请求
	put(url, data = {}, options = {}) {
		options.url = url
		options.data = data
		options.method = 'put'
		return this.request(options)
	},
	// head请求
	head(url, data = {}, options = {}) {
		options.url = url
		options.data = data
		options.method = 'head'
		return this.request(options)
	},
	// trace请求
	trace(url, data = {}, options = {}) {
		options.url = url
		options.data = data
		options.method = 'trace'
		return this.request(options)
	},
	// connect请求
	connect(url, data = {}, options = {}) {
		options.url = url
		options.data = data
		options.method = 'connect'
		return this.request(options)
	},
}